﻿using DayuCode.Designer.BLL;
using DayuCode.Designer.Entity;
using DayuCode.Designer.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DayuCode.Designer.Forms
{
    public class FuncTreeManager
    {
        Form owner;
        public FuncTreeManager(Form owner)
        {
            this.owner = owner;
        }

        /// <summary>
        /// 添加功能或者模块
        /// </summary>
        /// <param name="selectedFunc"></param>
        /// <param name="sblingOrChild">1 sbling , 2 child</param>
        public Func AddFunc(TreeView funcTree, long projectId, Func selectedFunc, int sblingOrChild)
        {
            int funcType = 0;// 1页面，2模块
            long parentId = -1;
            string parentName;
            bool maybeChild = true;
            if (selectedFunc == null)//增加跟节点
            {
                parentId = -1;
                parentName = "系统根模块";
            }
            else
            {
                if (selectedFunc.FuncType == 1)//选择节点是页面，则只能添加同级节点（但可以是页面或者模块）
                {
                    maybeChild = false;
                }
                if (maybeChild && 2 == sblingOrChild)
                {
                    parentId = selectedFunc.Id;
                    parentName = selectedFunc.Name;
                }
                else
                {
                    parentId = selectedFunc.ParentId;
                    if (parentId > 0)
                    {
                        Func pFunc = FuncHandler.getFunc(parentId);
                        parentName = pFunc.Name;
                    }
                    else
                    {
                        parentName = "系统根模块";
                    }
                }
            }

            FormFuncType form = new FormFuncType(sblingOrChild);
            form.Owner = this.owner;
            if (form.ShowDialog(this.owner) == DialogResult.OK)
            {
                funcType = form.FuncType;
            }
            form.Dispose();

            if (funcType == 0) return null;

            Func func = new Func();
            func.ProjectId = projectId;
            func.ParentId = parentId;
            func.FuncType = funcType;
            if (1 == funcType)//页面功能
            {
                FormNewFunc formFunc = new FormNewFunc(parentId, projectId, parentName);
                formFunc.Owner = this.owner;
                if(formFunc.ShowDialog(this.owner) == DialogResult.OK)
                {
                    func = formFunc.Func;
                }
                formFunc.Dispose();
            }
            else if (2 == funcType)//模块目录
            {
                FormFuncCatalog formFuncCatalog = new FormFuncCatalog(func);
                formFuncCatalog.Owner = this.owner;
                if (formFuncCatalog.ShowDialog(this.owner) == DialogResult.OK)
                {
                    func = formFuncCatalog.Func;
                }
                formFuncCatalog.Dispose();
            }
            LoadFuncTree(projectId, funcTree, func.Id);
            return func;
        }

        public Func EditFunc(TreeView funcTree, Func func)
        {
            if (1 == func.FuncType)
            {
                FormEditFunc formFunc = new FormEditFunc(func.Id);
                formFunc.Owner = this.owner;
                if (formFunc.ShowDialog(this.owner) == DialogResult.OK)
                {
                    func = formFunc.Func;
                }
                formFunc.Dispose();
            }
            else if (2 == func.FuncType)
            {
                FormFuncCatalog formFuncCatalog = new FormFuncCatalog(func);
                formFuncCatalog.Owner = this.owner;
                if (formFuncCatalog.ShowDialog(this.owner) == DialogResult.OK)
                {
                    func = formFuncCatalog.Func;
                }
                formFuncCatalog.Dispose();
            }
            LoadFuncTree(func.ProjectId, funcTree, func.Id);
            return func;
        }

        public void DeleteFunc(TreeView funcTree, Func func)
        {
            FuncHandler.deleteFunc(func.Id);
            LoadFuncTree(func.ProjectId, funcTree, func.ParentId);
        }

        public void LoadFuncTree(long projectId, TreeView funcTree, long selectedFuncId)
        {
            funcTree.Nodes.Clear();
            var funcs = FuncHandler.getAllFuncs(projectId);
            foreach (var func in funcs)
            {
                if (func.ParentId <= 0)
                {
                    var funcNode = funcTree.Nodes.Add(""+func.Id, func.Name);
                    funcNode.Tag = func;
                    int childCount = addFuncChildNodes(funcNode, func.Id, funcs);
                    if (childCount > 0 || func.FuncType == 2)
                    {
                        funcNode.ImageIndex = 0;
                        funcNode.SelectedImageIndex = 0;
                    }
                    else
                    {
                        int idx = (int)(func.Id % 10) + 1;
                        funcNode.ImageIndex = idx;
                        funcNode.SelectedImageIndex = idx;
                    }
                }
            }
            TreeNode[] nodes = funcTree.Nodes.Find("" + selectedFuncId, true);
            if (nodes.Length > 0)
            {
                funcTree.SelectedNode = nodes[0];
            }
        }

        private int addFuncChildNodes(TreeNode parentNode, long parentId, IList<Func> funcs)
        {
            int childCount = 0;
            foreach (var func in funcs)
            {
                if (func.ParentId == parentId)
                {
                    var funcNode = parentNode.Nodes.Add(""+func.Id, func.Name);
                    funcNode.Tag = func;
                    ++childCount;
                    int childCount2 = addFuncChildNodes(funcNode, func.Id, funcs);
                    if (childCount2 > 0 || func.FuncType == 2)
                    {
                        funcNode.ImageIndex = 0;
                        funcNode.SelectedImageIndex = 0;
                    }
                    else
                    {
                        int idx = (int)(func.Id % 10) + 1;
                        funcNode.ImageIndex = idx;
                        funcNode.SelectedImageIndex = idx;
                    }
                }
            }
            return childCount;
        }

    }
}
